package com.example.administrator.myapplication.net.chain;

import android.util.Log;

import com.example.administrator.myapplication.net.Request;
import com.example.administrator.myapplication.net.RequestBody;
import com.example.administrator.myapplication.net.Response;

import java.util.Map;

public class HeaderInterceptor implements Interceptor{
    String tag = "HeaderIntercptor";
    @Override
    public Response intercept(InterceptorChain chain) throws Exception {
        Log.e(tag,"请求头连接器");
        Request request = chain.call.request();
        Map<String, String> headers = request.headers();
        if (!headers.containsKey("Connection")) {
            headers.put("Connection","Keep-Alive");
        }
        headers.put("Host",request.url().getHost());
        if (request.body() != null) {
            RequestBody requestBody = request.body();
            long contentLength = requestBody.contentLength();
            if (contentLength>0){
                headers.put("Content-Length",String.valueOf(contentLength));
            }
        }
        return chain.process();
    }
}
